from selenium.webdriver.common.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
import time
class Base():
    def __init__(self,driver):
        self.driver=driver

    #调用驱动
    def open_ul(self,url):
        self.driver.get(url)

    #封装点击
    def click(self,loc):
        self.driver.find_element(loc).click()

    #封装输入
    def send_keys(self,loc):
        self.driver.find_element(loc).send_keys()

    #封装进入frname框架
    def infrname(self,loc):
        self.driver.switch_to.frame(loc)

    #退出框架
    def default_content(self,loc):
        self.driver.switch_to.default_content(loc)

    #滚动事件封装
    #滚动到顶部
    def scrollTo_top(self,):
        self.driver.execute_script('window.scrollTo(0,0)')

    #滚动到底部
    def scrollTo_bush(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollToHeight)')

    #滚动任意元素
    def scrollTo_div(self,s):
        self.driver.execute_script(f'window.scrollTo(0,{s})')

    #切换窗口
    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    #时间
    def time_pic(self, name):
        t = time.strftime("%Y%m%d%H%M%S", time.localtime())
        self.driver.save_screenshot(t + name)

    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.time_pic('error.png')
            print(e)